package com.ruoyi.web.controller.violation;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.RolesEnums;
import com.ruoyi.manger.domain.RegulationConfig;
import com.ruoyi.manger.service.IRegulationConfigService;
import com.ruoyi.system.dto.MapDto;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.util.BaiDuMapUtil;
import com.ruoyi.violation.vo.CorrectVo;
import com.ruoyi.violation.vo.LawAuditVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.violation.domain.ConstructionSiteViolationReport;
import com.ruoyi.violation.service.IConstructionSiteViolationReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目违章上报信息Controller
 * 
 * @author ruoyi
 * @date 2024-12-14
 */
@RestController
@RequestMapping("/violation/project")
public class ConstructionSiteViolationReportController extends BaseController
{
    @Autowired
    private IConstructionSiteViolationReportService constructionSiteViolationReportService;
    @Autowired
    private IRegulationConfigService iRegulationConfigService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询项目违章上报信息列表
     */
    @PreAuthorize("@ss.hasPermi('violation:project:list')")
    @GetMapping("/page")
    public TableDataInfo page(ConstructionSiteViolationReport constructionSiteViolationReport)
    {

        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        // 执法人员
        if(RolesEnums.ZHI_FA.getRoleKey().equals(roleKey)){
            constructionSiteViolationReport.setLawerUserId(user.getUserId());
        }

        startPage();
        List<ConstructionSiteViolationReport> list = constructionSiteViolationReportService.selectConstructionSiteViolationReportList(constructionSiteViolationReport);
        RegulationConfig config = new RegulationConfig();
        config.setType(0l);
        config.setStatus(0l);
        List<RegulationConfig> regulationConfigs = iRegulationConfigService.selectRegulationConfigList(config);
        Map<Long, RegulationConfig> configMap = regulationConfigs.stream().collect(Collectors.toMap(RegulationConfig::getId, Function.identity()));
        list.forEach(report->{
            report.setRegulationConfig(configMap.get(report.getConstructionViolationId()));
        });

        return getDataTable(list);
    }
    /**
     * 查询项目违章上报信息列表
     */
    @PreAuthorize("@ss.hasPermi('violation:project:list')")
    @GetMapping("/list")
    public AjaxResult list(ConstructionSiteViolationReport constructionSiteViolationReport)
    {
        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        // 执法人员
        if(RolesEnums.ZHI_FA.getRoleKey().equals(roleKey)){
            constructionSiteViolationReport.setLawerUserId(user.getUserId());
        }

        List<ConstructionSiteViolationReport> list = constructionSiteViolationReportService.selectConstructionSiteViolationReportList(constructionSiteViolationReport);
        RegulationConfig config = new RegulationConfig();
        config.setType(0l);
        config.setStatus(0l);
        List<RegulationConfig> regulationConfigs = iRegulationConfigService.selectRegulationConfigList(config);
        Map<Long, RegulationConfig> configMap = regulationConfigs.stream().collect(Collectors.toMap(RegulationConfig::getId, Function.identity()));
        list.forEach(report->{
            report.setRegulationConfig(configMap.get(report.getConstructionViolationId()));
        });
        return AjaxResult.success(list);
    }


    /**
     * 导出项目违章上报信息列表
     */
    @PreAuthorize("@ss.hasPermi('violation:project:export')")
    @Log(title = "项目违章上报信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConstructionSiteViolationReport constructionSiteViolationReport)
    {
        List<ConstructionSiteViolationReport> list = constructionSiteViolationReportService.selectConstructionSiteViolationReportList(constructionSiteViolationReport);
        ExcelUtil<ConstructionSiteViolationReport> util = new ExcelUtil<ConstructionSiteViolationReport>(ConstructionSiteViolationReport.class);
        util.exportExcel(response, list, "项目违章上报信息数据");
    }

    /**
     * 获取项目违章上报信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('violation:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        ConstructionSiteViolationReport constructionSiteViolationReport = constructionSiteViolationReportService.selectConstructionSiteViolationReportById(id);
        Long resolutionId = constructionSiteViolationReport.getConstructionViolationId();
        RegulationConfig regulationConfig = iRegulationConfigService.selectRegulationConfigById(resolutionId);
        constructionSiteViolationReport.setRegulationConfig(regulationConfig);
        SysDictData sysDictData = sysDictDataService.selectDictDataById(constructionSiteViolationReport.getViolationType());
        constructionSiteViolationReport.setViolationTypeName(sysDictData.getDictLabel());
        return success(constructionSiteViolationReport);
    }

    /**
     * 新增项目违章上报信息
     */
    @PreAuthorize("@ss.hasPermi('violation:project:add')")
    @Log(title = "项目违章上报信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConstructionSiteViolationReport constructionSiteViolationReport)
    {
        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        // 执法人员
        if(RolesEnums.ZHI_FA.getRoleKey().equals(roleKey)){
            constructionSiteViolationReport.setLawerUserId(user.getUserId());
            constructionSiteViolationReport.setLawerUserName(user.getUserName());

        }
        constructionSiteViolationReport.setReportNumber("PVR"+System.currentTimeMillis());
        String lat = constructionSiteViolationReport.getLat();
        String lon = constructionSiteViolationReport.getLon();
        MapDto mapDto = new MapDto();
        mapDto.setLat(lat);
        mapDto.setLon(lon);
        String address = BaiDuMapUtil.getAddress(mapDto);
        constructionSiteViolationReport.setLocAddress(address);

        return toAjax(constructionSiteViolationReportService.insertConstructionSiteViolationReport(constructionSiteViolationReport));
    }

    /**
     * 修改项目违章上报信息
     */
    @PreAuthorize("@ss.hasPermi('violation:project:edit')")
    @Log(title = "项目违章上报信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConstructionSiteViolationReport constructionSiteViolationReport)
    {
        String lat = constructionSiteViolationReport.getLat();
        String lon = constructionSiteViolationReport.getLon();
        MapDto mapDto = new MapDto();
        mapDto.setLat(lat);
        mapDto.setLon(lon);
        String address = BaiDuMapUtil.getAddress(mapDto);
        constructionSiteViolationReport.setLocAddress(address);
        return toAjax(constructionSiteViolationReportService.updateConstructionSiteViolationReport(constructionSiteViolationReport));
    }

    /**
     * 删除项目违章上报信息
     */
    @PreAuthorize("@ss.hasPermi('violation:project:remove')")
    @Log(title = "项目违章上报信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(constructionSiteViolationReportService.removeConstructionSiteViolationReportByIds(ids));
    }
    @PreAuthorize("@ss.hasPermi('violation:project:remove')")
    @Log(title = "审核", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody LawAuditVo lawAuditVo){
        lawAuditVo.setAuditUserId(getUserId());
        return toAjax(constructionSiteViolationReportService.audit(lawAuditVo));
    }
    @PreAuthorize("@ss.hasPermi('violation:vehicle:correct')")
    @Log(title = "整改", businessType = BusinessType.UPDATE)
    @PostMapping("/correct")
    public AjaxResult correct(@RequestBody CorrectVo correctVo){
        return toAjax(constructionSiteViolationReportService.correct(correctVo));
    }
}
